package com.guigu.qingcheng.service_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.user.TbAddress;
import com.guigu.qingcheng.service_user.mapper.TbAddressMapper;
import com.guigu.qingcheng.service_user.service.TbAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbAddressServiceImpl extends ServiceImpl<TbAddressMapper, TbAddress> implements TbAddressService {

    @Autowired
    private TbAddressService addressService;

    @Override
    public List<TbAddress> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbAddress> wrapper=queryWrapper(searchMap);
        List<TbAddress> list= addressService.list(wrapper);
        return list;
    }

    @Override
    public Page<TbAddress> findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        Page<TbAddress> page1=new Page<>(page,size);
        QueryWrapper<TbAddress> wrapper=queryWrapper(searchMap);
        IPage<TbAddress> list=addressService.page(page1,wrapper);
        return page1;
    }

    @Override
    public List<TbAddress> findByUsername(String username) {
        QueryWrapper<TbAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return addressService.list(wrapper);
    }

    /**
     * 高级组合查询封装对象
     * @param searchMap
     * @return
     */
    public QueryWrapper<TbAddress> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbAddress> wapper = new QueryWrapper<TbAddress>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("username") != null && !"".equals(searchMap.get("username")))
                wapper.like("username", "%" + searchMap.get("username") + "%");
            if(searchMap.get("provinceid") != null && !"".equals(searchMap.get("provinceid")))
                wapper.eq("provinceid", searchMap.get("provinceid"));
            if(searchMap.get("cityid") != null && !"".equals(searchMap.get("cityid")))
                wapper.eq("cityid", searchMap.get("cityid"));
            if(searchMap.get("areaid") != null && !"".equals(searchMap.get("areaid")))
                wapper.eq("areaid", searchMap.get("areaid"));
            if(searchMap.get("isDefault") != null && !"".equals(searchMap.get("isDefault")))
                wapper.eq("isDefault", searchMap.get("isDefault"));
            if(searchMap.get("phone") != null && !"".equals(searchMap.get("phone")))
                wapper.like("phone", "%" + searchMap.get("phone") + "%");
            if(searchMap.get("address") != null && !"".equals(searchMap.get("address")))
                wapper.like("address", "%" + searchMap.get("address") + "%");
            if(searchMap.get("contact") != null && !"".equals(searchMap.get("contact")))
                wapper.like("contact", "%" + searchMap.get("contact") + "%");
            if(searchMap.get("alias") != null && !"".equals(searchMap.get("alias")))
                wapper.like("alias", "%" + searchMap.get("alias") + "%");

        }
        return wapper;
    }


}
